package popcorn.ticket.controller;

import java.io.IOException;
import java.util.Random;

import javax.servlet.http.HttpSession;

import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import popcorn.ticket.service.UsersService;
import popcorn.ticket.tool.AjaxResponse;
/**
 * 短信控制器
 * @author 陈浩
 *
 */
@RestController
public class MesApiController {
	Logger logger = Logger.getRootLogger();
	@Autowired
	UsersService usersService;

	@RequestMapping(value = "sendApi/{usertel}")
	public AjaxResponse sendApi(@PathVariable("usertel") String usertel, HttpSession session)
			throws HttpException, IOException {
		String msg = "手机号不存在";
		boolean flag = usersService.getInfoByTel(usertel);
		// 判断改电话号码是否存在
		if (flag) {
			// 随机生成一组验证码
			Random random = new Random();
			String num = "";
			for (int i = 0; i < 6; i++) {
				num += random.nextInt(10);
			}
//			HttpClient client = new HttpClient();
			PostMethod post = new PostMethod("http://gbk.api.smschinese.cn");
			post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=gbk");// 在头文件中设置转码
				NameValuePair[] data = { new NameValuePair("Uid", "南柯一梦8023"),
						new NameValuePair("Key", "d41d8cd98f00b204e980"), new NameValuePair("smsMob", usertel),
						new NameValuePair("smsText", "验证码：" + num+"【爆米花公司】") };
				logger.debug("debug->data :" + data);
			logger.debug("************************************debug->num******************************** :");
			logger.debug("debug->num :" + num);
			logger.debug("************************************debug->num******************************** :");

			//保存验证码
			session.setAttribute("pwd", num);
			//保存手机号
			session.setAttribute("usertel", usertel);
			msg = "验证码发送成功";
			AjaxResponse aj = new AjaxResponse(msg, Integer.parseInt(num));
			return aj;
		}
		AjaxResponse aj = new AjaxResponse(msg, 200);
		return aj;

	}

	// 判断验证码是否正确
	@RequestMapping(value = "getApi/{yzm}")
	public AjaxResponse getApi(@PathVariable("yzm") String yzm, HttpSession session) {
		// 获得保存在session中的验证码
		String pwd = (String) session.getAttribute("pwd");
		String msg = "";
		// 判断用户端输入的验证码是否正确
		if (yzm.equals(pwd)) {
			AjaxResponse ajaxResponse = new AjaxResponse(msg, 200);
			return ajaxResponse;
		}
		AjaxResponse ajaxResponse = new AjaxResponse(msg, 500);
		return ajaxResponse;
	}

	// 修改密码
	@RequestMapping(value = "updPwd/{userpwd}")
	public AjaxResponse updPwd(@PathVariable("userpwd") String userpwd, HttpSession session) {
		logger.debug("修改："+userpwd);
		// 获得保存在session中的手机号
		String tel = (String) session.getAttribute("usertel");
		String msg = "修改失败";
		// 判断密码修改是否成功
		boolean flag = usersService.updPwd(tel, userpwd);
		if (flag) {
			msg="修改成功";
			AjaxResponse ajaxResponse = new AjaxResponse(msg, 200);
			return ajaxResponse;
		}
		AjaxResponse ajaxResponse = new AjaxResponse(msg, 500);
		return ajaxResponse;
	}
}
